
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {getUserList, toEditUpdateInfo, toDeleteInfo, toAddUserInfo} from './service';


import {SingleUserInfoType} from './data';

export interface UserListDataType {
  data: SingleUserInfoType[];
  meta: {
    total: number;
    per_page: number;
    page: number;
  }
}


interface UserModelType {
  namespace: 'userListDemo';
  state: UserListDataType;
  reducers: {
    updateDemoData: Reducer<UserListDataType>;
  };
  effects: {
    queryRemoteDemoUserList: Effect;
    editUpdateInfo: Effect;
    deleteUserInfo: Effect;
    addUserInfo: Effect;
  },
  subscriptions: {
    setup: Subscription,
  }
}



const UserModel: UserModelType = {

  namespace: 'userListDemo',

  state: {
    data: [],
    meta: {
      total: 0,
      per_page: 10,
      page: 1,
    }
  },

  reducers: {
    updateDemoData(state, {type, payload}) {

      // return {
      //   ...state,
      //   ...action.payload,
      // };

      console.log('reducers更新状态', payload);
      return payload
    },

    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },

  effects: {
    // action结构如下：  { type: 'saveData', payload: {} }
    // effects结构如下（call、put均为函数类型）： { call: function , put: function }
    *queryRemoteDemoUserList({ payload }, { call, put }) {

      // console.log('call为', typeof call, 'put为', typeof put);

      // 注意： 此刻必须要有 yield call
      const data = yield call(getUserList);
      console.log('model层--获取列表', data);
      if (data){
        // 注意： 这里的data必须要封装成对象的形式！！！
        yield put({
          type: 'updateDemoData',
          payload: data
        })
      }
    },




    *editUpdateInfo( { payload: {id, values} }, { call, put }) {
      console.log('编辑更新',values, id);
      const  res = yield call(toEditUpdateInfo, {id, values});
      console.log('model层----编辑更新', res);

      // 重新刷新一下列表（调用effects中其他的type，即effect之间相互调用使用）
      yield put({
        type: 'queryRemoteDemoUserList'
      })

    },


    *deleteUserInfo({payload: {id}}, { call, put}) {
      console.log('删除用户', id);
      const data = yield call(toDeleteInfo, {id});
      console.log('model层----删除', data);

      yield put({
        type: 'queryRemoteDemoUserList'
      })
    },


    *addUserInfo({payload: {values}}, {call, put}){
      console.log('添加传参', values);
      const data = yield call(toAddUserInfo, {values});
      yield put({
        type: 'queryRemoteDemoUserList'
      })
    }





  },


  subscriptions: {
    setup({ dispatch, history }, done: any) {
      return history.listen((location, action) => {
        if (location.pathname === '/users') {

          // 由于dispatch分发action，则action是一个对象。形如： { type: 'queryRemoteDemoUserList', payload: {} }
          dispatch({
            // 异步
            type: 'queryRemoteDemoUserList',

            // 同步
            // type: 'updateDemoData',
          });


        }
      });
    },
  },
};

export default UserModel;
